<?php  
	include("../includes/connect_db.php");
	include("../includes/core.php");

	$act = $_POST['act'];

	switch ($act) {
		case '1':
			$resto_menu = $_POST['resto_menu'];
			$msg = "sukses";
			foreach ($resto_menu as $value) {
        $sql_update = "UPDATE resto_menu SET harga = '$value[1]' WHERE id = '$value[0]'";
        if ($query_update = mysql_query($sql_update)) {
        } else {
        	$msg = "gagal";
        }
      }
      echo $msg;

			break;

		case '2':
			$bill = $_POST['bill'];
			$bill_1 = $_POST['bill_1'];
			$bill_2 = $_POST['bill_2'];
			$idresto = $_POST['idresto'];

			$bill_1_len = count($bill_1);
			$bill_2_len = count($bill_2);

			$i = 0;
			$sql_resto_menu = "SELECT * FROM resto_menu WHERE idresto = '$idresto' AND bill = '1'";
			$query_resto_menu = mysql_query($sql_resto_menu);
			while ($resto_menu = mysql_fetch_array($query_resto_menu)) {
				if ($i < $bill_1_len) {
					$idmenu = $bill_1[$i][0];
					$jumlah = $bill_1[$i][1];
					$harga = $bill_1[$i][2];

					$sql_menu = "SELECT * FROM menu WHERE id = '$idmenu'";
					$query_menu = mysql_query($sql_menu);
					$menu = mysql_fetch_array($query_menu);

					$sql_update = "UPDATE resto_menu SET idmenu = '$idmenu', jumlah = '$jumlah', harga = '$harga', harga_ori = '$menu[harga]' WHERE id = '$resto_menu[id]'";
					$query_update = mysql_query($sql_update);
				} else {
					$sql_delete = "DELETE FROM resto_menu WHERE id = '$resto_menu[id]'";
					$query_delete = mysql_query($sql_delete);
				}
				$i++;
			}

			for ($i=0; $i < $bill_2_len; $i++) { 
				$idmenu = $bill_2[$i][0];
				$jumlah = $bill_2[$i][1];
				$harga = $bill_2[$i][2];

				$sql_insert = "INSERT INTO resto_menu (idresto, idmenu, harga, jumlah, bill) VALUES ('$idresto', '$idmenu', '$harga', '$jumlah', '$bill')";
				$query_insert = mysql_query($sql_insert);
			}
			
			echo "sukses";

			break;

		case '3':
			$bill = $_POST['bill'];
			$idresto = $_POST['idresto'];

			$sql_old = "SELECT * FROM resto_menu WHERE idresto = '$idresto' AND bill = '$bill'";
			$query_old = mysql_query($sql_old);
			while ($result_old = mysql_fetch_array($query_old)) {
				$sql_select = "SELECT * FROM resto_menu WHERE idresto = '$idresto' AND idmenu = '$result_old[idmenu]' AND bill = '1'";
				$query_Select = mysql_query($sql_select);
				$result_select = mysql_fetch_array($query_Select);

				$jumlah = $result_select[jumlah] + $result_old[jumlah];

				$sql_update = "UPDATE resto_menu SET jumlah = '$jumlah' WHERE id = '$result_select[id]'";
				$query_update = mysql_query($sql_update);

				$sql_delete = "DELETE FROM resto_menu WHERE idresto = '$idresto' AND bill = '$bill'";
				$query_delete = mysql_query($sql_delete);
			}

			$sql_select_bill = "SELECT * FROM resto_menu WHERE bill > '$bill' AND idresto = '$idresto'";
			$query_select_bill = mysql_query($sql_select_bill);
			if (mysql_num_rows($query_select_bill)> 0) {
				$query_select_bill = mysql_query($sql_select_bill);
				while ($select_bill = mysql_fetch_array($query_select_bill)) {
					$new_bill = $select_bill['bill'] - 1;
					$sql_update_bill = "UPDATE resto_menu SET bill = '$new_bill' WHERE bill = '$select_bill[bill]' AND idresto = '$idresto'";
					$query_update_bill = mysql_query($sql_update_bill);
				}
			}

			echo "sukses";
			break;

		default:
			# code...
			break;
	}
?>